<template>
  <div>
    <a-dropdown v-if="currentUser && currentUser.name" placement="bottomRight">
      <span class="ant-pro-account-avatar">
        <a-avatar size="small" :src="userInfo.avatar" style="margin-right: 5px;" />
        <span>{{ userInfo.name || userInfo.info.name || userInfo.info.username }}</span>
      </span>
      <template v-slot:overlay>
        <a-menu class="ant-pro-drop-down menu" :selected-keys="[]">
          <!-- <a-menu-item v-if="menu" key="center" @click="handleToCenter">
            <a-icon type="user" />
            个人中心
          </a-menu-item>
          <a-menu-item v-if="menu" key="settings" @click="handleToSettings">
            <a-icon type="setting" />
            个人设置
          </a-menu-item>
          <a-menu-divider v-if="menu" /> -->
          <a-menu-item key="editPassword" @click="handleEditPassword">
            <a-icon type="edit" />
            修改密码
          </a-menu-item>
          <a-menu-item key="logout" @click="handleLogout">
            <a-icon type="logout" />
            退出登录
          </a-menu-item>
        </a-menu>
      </template>
    </a-dropdown>
    <span v-else>
      <a-spin size="small" :style="{ marginLeft: 8, marginRight: 8 }" />
    </span>
    <a-modal v-model="visibleEditPassword" title="修改密码" @ok="handleEditPasswordOk" :confirmLoading="confirmLoading">
      <a-form-model
        ref="editPasswordForm"
        :rules="editPasswordRules"
        :model="editPasswordModel"
        :label-col="labelCol"
        :wrapper-col="wrapperCol"
      >
        <a-form-model-item label="关联手机号" has-feedback prop="mobile" v-if="!userInfo.info.is_platform && userInfo.info.username !='admin'">
          <a-input
            v-model="editPasswordModel.mobile"
          />
        </a-form-model-item>
        <a-form-model-item label="原密码" has-feedback prop="password">
          <a-input-password
            placeholder="请输入原密码"
            v-model="editPasswordModel.password"
            type="password"
            autocomplete="off"
          />
        </a-form-model-item>
        <a-form-model-item label="新密码" has-feedback prop="newPassword">
          <a-input-password
            placeholder="请输入新密码"
            v-model="editPasswordModel.newPassword"
            type="password"
            autocomplete="off"
          />
        </a-form-model-item>
        <a-form-model-item label="确认新密码" has-feedback prop="confirmPassword">
          <a-input-password
            placeholder="确认新密码"
            v-model="editPasswordModel.confirmPassword"
            type="password"
            autocomplete="off"
          />
        </a-form-model-item>
      </a-form-model>
    </a-modal>
  </div>
</template>

<script>
import { Modal } from 'ant-design-vue'
import { editPassword } from '@/api/auth'
import storage from 'store'
export default {
  name: 'AvatarDropdown',
  data() {
    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入新密码'))
      } else {
        if (this.editPasswordModel.confirmPassword !== '') {
          this.$refs.editPasswordForm.validateField('confirmPassword')
        }
        callback()
      }
    }
    const validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入确认密码'))
      } else if (value !== this.editPasswordModel.newPassword) {
        callback(new Error('两次密码不匹配!'))
      } else {
        callback()
      }
    }
    return {
      confirmLoading: false,
      visibleEditPassword: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 19 }
      },
      editPasswordModel: {
        mobile:'',
        password: '',
        newPassword: '',
        confirmPassword: ''
      },
      editPasswordRules: {
        mobile: [{ required: true, message: '请输入手机端关联手机号', trigger: 'blur' }],
        password: [{ required: true, message: '请输入原密码', trigger: 'blur' }],
        newPassword: [{ validator: validatePass, trigger: 'change' }],
        confirmPassword: [{ validator: validatePass2, trigger: 'change' }]
      }
    }
  },
  props: {
    currentUser: {
      type: Object,
      default: () => null
    },
    menu: {
      type: Boolean,
      default: true
    }
  },
  computed: {
    userInfo() {
      return this.$store.state.user
    }
  },
  methods: {
    handleEditPasswordOk: function() {
      this.$refs['editPasswordForm'].validate(valid => {
        if (valid) {
          this.confirmLoading = true
          editPassword(this.editPasswordModel)
            .then(res => {
              if (res.code == 0) {
                this.visibleEditPassword = false
                this.$message.success(res.msg)
              }
            })
            .catch(error => {
              console.log(error)
            })
            .then(() => {
              this.confirmLoading = false
            })
        }
      })
    },
    handleEditPassword: function(e) {
      this.visibleEditPassword = true
      if(!this.userInfo.info.is_platform) {
        this.editPasswordModel.mobile = this.userInfo.info.mobile
      }
    },
    handleLogout(e) {
      Modal.confirm({
        title: this.$t('layouts.usermenu.dialog.title'),
        content: this.$t('layouts.usermenu.dialog.content'),
        onOk: () => {
          // return new Promise((resolve, reject) => {
          //   setTimeout(Math.random() > 0.5 ? resolve : reject, 1500)
          // }).catch(() => console.log('Oops errors!'))
          return this.$store.dispatch('Logout').then(() => {
            setTimeout(() => {
              window.location.reload()
            }, 500)
            const toLoginPath = localStorage.getItem('defaultLoginPath')
            this.$router.push({ path: toLoginPath })
          })
        },
        onCancel() {}
      })
    }
  },
  mounted() {
    console.log(this.userInfo,"userInfo");
  },
}
</script>

<style lang="less" scoped>
.ant-pro-drop-down {
  /deep/ .action {
    margin-right: 8px;
  }
  /deep/ .ant-dropdown-menu-item {
    min-width: 160px;
  }
}

.ant-pro-account-avatar {
  display: flex;
  align-items: center;
}
</style>
